<?php

Class Event {

	private $record, $record_orig, $records;
	public $db, $_test;
	
	function __construct() {
		$this->_test = new Test();
	}
	
/**
* Returns a since field value
* @return mixed
*/		
	public function get( $field ) {
				
		if (isset($this->record[$field])) {
			return( $this->record[$field] );
		} else {
			return( false );
		}
	}

/**
* Set the value to a field
* @return bool
*/		
	public function set( $field, $value ) {
	
		$this->record[$field] = $value;
		return( true );
		
	}

/**
* Sets the filtered data to the record array
* @param mixed : $data : array of filtered inputs
* @return bool
*/

	public function setData($data) {

		if(count($data)) {
			foreach($data as $key => $value) {
				$this->record[$key] = $value;
			}
		}
		return( true );
	}
 
/**
* Returns all values for project
* @return array
*/		
	public function get_all( $set = 'NEW' ) {

		if ($set == 'NEW') {
			return( $this->record );
		}

		if ($set == 'ORIG') {
			return( $this->record_orig );
		}
		
		return( false );
		
	}

/**
 * Get the total record count of the last executed select query
 */
	public function getTotal() {
		return $this->total;
	}

	/**
	* Loads the event by id
	*/		
	public function load_by_id($event_id) {
		$query = sprintf("SELECT * FROM `event` WHERE `event_id` = %s ", mysql_escape_string($event_id) );
		$ret = $this->db->query_one( $query );
		unset($this->records);
		if ($ret != NULL) {
			$records = array();
			foreach( $ret as $field => $value ) {
				$this->record_orig[$field] = $value;
				$this->record[$field] = $value;
			}
			$this->records[] = $this->record;
			return(true);
		} else {
			return(false);
		}
	}

	/**
	*
	*/
	public function save() {
	
		if($this->recordExists($this->get('event_id'))){
			$query = sprintf(" UPDATE `event` SET `title` = '%s', `event_type_id` = '%s', `date_start` = '%s', `date_end` = '%s', `description` = '%s', `land_id` = '%s' WHERE `event_id` = %s "
				, $this->get('title')
				, $this->get('event_type_id')
				, $this->get('date_start')
				, $this->get('date_end')
				, $this->get('description')
				, $this->get('land_id')
				, $this->get('event_id')
			);

		} else {
			$query = sprintf("INSERT INTO `event` SET `title` = '%s', `event_type_id` = '%s', `date_start` = '%s', `date_end` = '%s', `description` = '%s', `land_id` = '%s' "
				, $this->get('title')
				, $this->get('event_type_id')
				, $this->get('date_start')
				, $this->get('date_end')
				, $this->get('description')
				, $this->get('land_id')
			);
		}

		try {
			$result = $this->db->query($query);
		} catch (Exception $e) {
			trigger_error($e->getMessage(),E_USER_ERROR);
		}
		return ( $result );

	}

	public function removeEvent( $event_id, $user_id = -1 ) {
		$query = sprintf("DELETE e FROM event e JOIN user_land u ON e.land_id = u.land_id AND u.user_id = %s WHERE event_id = %s"
			,	mysql_escape_string($user_id)
			,	mysql_escape_string($event_id)
		);

#		print $query;
		$record = $this->db->query($query);
		if($this->db->affected_rows > 0) {
			return true;
		}
		return false;
	}

	/**
	*checks whether record exists in database
	*/
	public function recordExists ($event_id){

		if ($event_id == 0) {
			return false;
		}
			
		$query = sprintf("SELECT `event_id` FROM `event` WHERE `event_id` = '%s';", mysql_escape_string($event_id) );

		$ret = $this->db->query_one( $query );
		if ($ret == NULL) {
			return false;
		} else {
			return true;
		}
	}

	public function getList (){

		$query = sprintf("SELECT `event_id`, `%s` as value FROM `event` WHERE `%s` LIKE '%s%%' ORDER BY `%s` %s "
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('value'))
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('sort'))
		);
		if( $this->get('start') && $this->get('limit') ) {
			$where = sprintf(" LIMIT %s, %s"
				,mysql_escape_string($this->get('start'))
				,mysql_escape_string($this->get('limit'))
				);
			$query .= $where;
		}

		$ret = $this->db->query_all($query);
		$this->total = $this->db->query_total();
		return $ret;

	}

	public function getEventTypeList() {

		$query = sprintf("SELECT `event_type_id`, `%s` as value FROM `event_type` WHERE `%s` LIKE '%s%%' ORDER BY `%s` %s "
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('value'))
			, mysql_escape_string($this->get('field'))
			, mysql_escape_string($this->get('sort'))
		);
		if( $this->get('start') && $this->get('limit') ) {
			$where = sprintf(" LIMIT %s, %s"
				,mysql_escape_string($this->get('start'))
				,mysql_escape_string($this->get('limit'))
				);
			$query .= $where;
		}

		$ret = $this->db->query_all($query);
		$this->total = $this->db->query_total();
		return $ret;

	}

	public function listRecords (){
		if($this->get('filter')) {
			$where = build_where($this->get('filter'), 'event.', $this->getValidFields());
		}
		if ($where != '') {
			$where = " AND " . $where;
		}
		if($this->get('order') != '') {
			$where .= build_order( $this->get('order'), 'event.', $this->getValidFields() );
		}
		$where .= build_limit( $this->get('start'), $this->get('limit') );

		$query = " SELECT SQL_CALC_FOUND_ROWS DISTINCT land.title as property, event.*, event_type.title as event_type FROM land, users, user_land, event, event_type WHERE  users.id = user_land.user_id AND event.land_id = land.land_id AND event.event_type_id = event_type.event_type_id ";
		
		if($this->get('userFilter') == 'true' && $this->get('user_id') != ''){
				$query .=  sprintf(" AND user_land.land_id = land.land_id AND users.id = %s ", $this->get('user_id'));
		}
		$query .= $where;

		try {
//print $query;			
			$ret = $this->db->query_all( $query );
			$this->total = $this->db->query_total();
		} catch (Exception $e) {
			trigger_error($e->getMessage(),E_USER_ERROR);
		}
		
		return $ret;
	}

	public function getValidFields() {
			$ar = array( 'title', 'event_type_id', 'date_start', 'date_end', 'land_id');
			return $ar;
	}
	
}

?>